﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

namespace Hxsoft.AppV1
{
	public class UserControl : System.Web.UI.UserControl, IIncludeFile
	{
		public List<PageMessage> Messages
		{
			get { return ApplicationPage.Messages; }
		}

		public Page ApplicationPage
		{
			get
			{
				return base.Page as Page;
			}
		}

		private LoadedValue<string> loadedItemDefaultImageUrl;
		/// <summary>
		/// 列表项的默认图片地址。
		/// </summary>
		public virtual string ItemDefaultImageUrl
		{
			get
			{
				if (loadedItemDefaultImageUrl.Loader == null)
					loadedItemDefaultImageUrl.Loader = () =>
					{
						if (ViewState["ItemDefaultImageUrl"] == null)
							return "/_Client/Controls/ResourcePopupSelector/NotImage.jpg";
						return (string)ViewState["ItemDefaultImageUrl"];
					};
				return loadedItemDefaultImageUrl.Value;
			}
			set
			{
				if (loadedItemDefaultImageUrl.Setter == null)
					loadedItemDefaultImageUrl.Setter = v => ViewState["ItemDefaultImageUrl"] = v;
				loadedItemDefaultImageUrl.Value = value;
			}
		}


		protected virtual Dictionary<string, IncludeFileType> HeadIncludeFiles
		{
			get
			{
				return null;
			}
		}

		public Dictionary<string, IncludeFileType> GetHeadIncludeFiles()
		{
			Dictionary<string, IncludeFileType> files = new Dictionary<string, IncludeFileType>(HeadIncludeFiles);

			foreach (var control in this.Controls)
			{
				UserControl uControl = control as UserControl;
				if (uControl != null)
				{
					Dictionary<string, IncludeFileType> controlFiles = uControl.GetHeadIncludeFiles();
					if (controlFiles != null)
					{
						foreach (var controlFile in controlFiles)
						{
							if (!files.ContainsKey(controlFile.Key))
								files[controlFile.Key] = controlFile.Value;
						}
					}
				}
			}

			return files;
		}
	}
}